package com.woniu.inlet.web.controller;

import com.woniu.exception.RiskException;
import com.woniu.inlet.web.command.RiskCommand;
import com.woniu.inlet.web.vo.RiskVo;
import com.woniu.response.ResponseResult;
import com.woniu.service.RiskService;
import com.woniu.service.domain.RiskDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api("风险测评相关测试")
@Slf4j
@CrossOrigin
public class RiskController {

    @Autowired
    private RiskService riskService;

    @GetMapping("/user/risk")
    @ApiOperation("用户风险测评")
    public ResponseResult<Object> risk(Integer userId, Integer score) {
        try {
            RiskCommand command = new RiskCommand(userId, score);
            RiskDto riskDto = command.execute();
            log.debug("riskDto是[{}]", riskDto);
            RiskVo riskVo = new RiskVo();
            BeanUtils.copyProperties(riskDto, riskVo);
            log.debug("riskVo是[{}]", riskVo);
            return ResponseResult.ok(riskVo);
        } catch (Exception e) {
            if (e instanceof RiskException) {
                return new ResponseResult<>(200, e.getMessage());
            } else {
                return ResponseResult.fail("风险测评系统维护中。。。");
            }

        }
    }

    @GetMapping("/user/risk/{riskId}")
    @ApiOperation("根据风险测评结果id查询")
    public ResponseResult getRiskById(@PathVariable("riskId") Integer riskId) {

        try {
            RiskDto riskDto = riskService.getById(riskId);
            RiskVo riskVo = new RiskVo();
            BeanUtils.copyProperties(riskDto, riskVo);
            return ResponseResult.ok(riskVo);
        } catch (Exception e) {
            return ResponseResult.fail("风险测评查询失败，系统维护中。。");
        }

    }
}
